{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyNhZ6Tq/f9o6VQamLDD7K+G",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/sugarforever/LangChain-Tutorials/blob/main/LangChain_SQLDatabaseChain_Vulnerability.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# LangChain SQL Agent"
      ],
      "metadata": {
        "id": "k1D8nvXe4Bo0"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "The vulnerability in `SQLDatabaseChain` may be exploited if it's carefully used.\n",
        "\n",
        "In this tutorial, I will show you the PoC and introduce you a safer way to interact with SQL databases using natural language - **SQL Agent**."
      ],
      "metadata": {
        "id": "SL-f_6C74G2Z"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "1. Set up the development environment"
      ],
      "metadata": {
        "id": "CS-iP2zE5Wmw"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "XIqvTvsPol9-"
      },
      "outputs": [],
      "source": [
        "!pip install -q -U langchain langchain_experimental openai"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain import OpenAI, SQLDatabase\n",
        "from langchain_experimental.sql import SQLDatabaseChain"
      ],
      "metadata": {
        "id": "DXTFTS_Ao0ki"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "2. Get the OpenAI API key ready"
      ],
      "metadata": {
        "id": "I-PZvNsJ5bw2"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "os.environ['OPENAI_API_KEY'] = 'your valid openai api key'"
      ],
      "metadata": {
        "id": "m7mGK3DDr0A0"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "3. Download a SQLite dataset"
      ],
      "metadata": {
        "id": "ymEID1JK5hEp"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!wget https://bafybeibqw3muonedrl57jvmhh2oemftjvamtc22htygi7vl52qmgbvqmk4.ipfs.nftstorage.link/retailDB.sqlite -O retail.sqlite"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "qU9fdfuz2omV",
        "outputId": "d7f136c6-39d2-4b22-a9bd-37ff73f175f1"
      },
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "--2023-09-24 21:17:34--  https://bafybeibqw3muonedrl57jvmhh2oemftjvamtc22htygi7vl52qmgbvqmk4.ipfs.nftstorage.link/retailDB.sqlite\n",
            "Resolving bafybeibqw3muonedrl57jvmhh2oemftjvamtc22htygi7vl52qmgbvqmk4.ipfs.nftstorage.link (bafybeibqw3muonedrl57jvmhh2oemftjvamtc22htygi7vl52qmgbvqmk4.ipfs.nftstorage.link)... 104.18.41.40, 172.64.146.216, 2606:4700:4400::6812:2928, ...\n",
            "Connecting to bafybeibqw3muonedrl57jvmhh2oemftjvamtc22htygi7vl52qmgbvqmk4.ipfs.nftstorage.link (bafybeibqw3muonedrl57jvmhh2oemftjvamtc22htygi7vl52qmgbvqmk4.ipfs.nftstorage.link)|104.18.41.40|:443... connected.\n",
            "HTTP request sent, awaiting response... 200 OK\n",
            "Length: 2940928 (2.8M) [text/plain]\n",
            "Saving to: ‘retail.sqlite’\n",
            "\n",
            "retail.sqlite       100%[===================>]   2.80M  --.-KB/s    in 0.04s   \n",
            "\n",
            "2023-09-24 21:17:36 (79.8 MB/s) - ‘retail.sqlite’ saved [2940928/2940928]\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "4. Instantiate a SQLDatabase instance"
      ],
      "metadata": {
        "id": "A301zrQw5llY"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain.utilities import SQLDatabase\n",
        "from langchain.llms import OpenAI\n",
        "from langchain_experimental.sql import SQLDatabaseChain\n",
        "\n",
        "db = SQLDatabase.from_uri(\"sqlite:///retail.sqlite\")\n",
        "llm = OpenAI(temperature=0, verbose=True)"
      ],
      "metadata": {
        "id": "KPFAaziN2kBr"
      },
      "execution_count": 9,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)"
      ],
      "metadata": {
        "id": "CTnekNwV3URo"
      },
      "execution_count": 10,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "5. Interact with database using `SQLDatabaseChain`\n",
        "\n",
        "Now we are going to interact with database. We will try to update and delete data from database which will cause data change."
      ],
      "metadata": {
        "id": "d8gPLwax5rMW"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "5.1 Modify data by SQLDatabaseChain"
      ],
      "metadata": {
        "id": "SLul4pF055GK"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "db_chain.run(\"Change all the real reviews to -1\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 183
        },
        "id": "oO8rGXah326R",
        "outputId": "2685aac4-792f-406f-9757-10c56959c925"
      },
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "\n",
            "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
            "Change all the real reviews to -1\n",
            "SQLQuery:\u001b[32;1m\u001b[1;3mUPDATE reviews SET real_reviews = -1;\u001b[0m\n",
            "SQLResult: \u001b[33;1m\u001b[1;3m\u001b[0m\n",
            "Answer:\u001b[32;1m\u001b[1;3mAll the real reviews have been changed to -1.\u001b[0m\n",
            "\u001b[1m> Finished chain.\u001b[0m\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'All the real reviews have been changed to -1.'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "db_chain.run(\"list the first row in reviews table\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 183
        },
        "id": "Jbyymb-T6nMO",
        "outputId": "9b2e2d76-103a-4524-ef9e-e48b9eaf3f80"
      },
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "\n",
            "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
            "list the first row in reviews table\n",
            "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT \"product_id\", \"rating\", \"reviews\", \"Hour\", \"minute\", \"real_rating\", \"real_reviews\", \"Unnamed: 7\" FROM reviews LIMIT 1;\u001b[0m\n",
            "SQLResult: \u001b[33;1m\u001b[1;3m[('AH2430', 'None', 'None', 0.0, 0.0, None, -1.0, '')]\u001b[0m\n",
            "Answer:\u001b[32;1m\u001b[1;3mThe first row in reviews table is ('AH2430', 'None', 'None', 0.0, 0.0, None, -1.0, '').\u001b[0m\n",
            "\u001b[1m> Finished chain.\u001b[0m\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\"The first row in reviews table is ('AH2430', 'None', 'None', 0.0, 0.0, None, -1.0, '').\""
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 12
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "5.2 Delete data"
      ],
      "metadata": {
        "id": "ssPUoOzj59GA"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "db_chain.run(\"Delete all the reviews\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 183
        },
        "id": "lsRU4uKf3swW",
        "outputId": "e4fbe5e4-39ca-4b6a-b6e4-943dcbf15e25"
      },
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "\n",
            "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
            "Delete all the reviews\n",
            "SQLQuery:\u001b[32;1m\u001b[1;3mDELETE FROM reviews;\u001b[0m\n",
            "SQLResult: \u001b[33;1m\u001b[1;3m\u001b[0m\n",
            "Answer:\u001b[32;1m\u001b[1;3mAll reviews have been deleted.\u001b[0m\n",
            "\u001b[1m> Finished chain.\u001b[0m\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'All reviews have been deleted.'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 13
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "db_chain.run(\"show me the count of reviews\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 183
        },
        "id": "u0HmHqWx6xuA",
        "outputId": "f03e6846-283f-4d6b-b58c-b396d60b8bba"
      },
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "\n",
            "\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
            "show me the count of reviews\n",
            "SQLQuery:\u001b[32;1m\u001b[1;3mSELECT COUNT(*) FROM reviews;\u001b[0m\n",
            "SQLResult: \u001b[33;1m\u001b[1;3m[(0,)]\u001b[0m\n",
            "Answer:\u001b[32;1m\u001b[1;3mThere are 0 reviews.\u001b[0m\n",
            "\u001b[1m> Finished chain.\u001b[0m\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'There are 0 reviews.'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "6. Interact with database using **SQL Agent**\n"
      ],
      "metadata": {
        "id": "RqKpnJQ15_P9"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!wget https://bafybeibqw3muonedrl57jvmhh2oemftjvamtc22htygi7vl52qmgbvqmk4.ipfs.nftstorage.link/retailDB.sqlite -O retail2.sqlite"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "lHL43lvv723L",
        "outputId": "88905726-2c8d-4e20-8bec-1a7f83885264"
      },
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "--2023-09-24 21:20:52--  https://bafybeibqw3muonedrl57jvmhh2oemftjvamtc22htygi7vl52qmgbvqmk4.ipfs.nftstorage.link/retailDB.sqlite\n",
            "Resolving bafybeibqw3muonedrl57jvmhh2oemftjvamtc22htygi7vl52qmgbvqmk4.ipfs.nftstorage.link (bafybeibqw3muonedrl57jvmhh2oemftjvamtc22htygi7vl52qmgbvqmk4.ipfs.nftstorage.link)... 104.18.41.40, 172.64.146.216, 2606:4700:4400::ac40:92d8, ...\n",
            "Connecting to bafybeibqw3muonedrl57jvmhh2oemftjvamtc22htygi7vl52qmgbvqmk4.ipfs.nftstorage.link (bafybeibqw3muonedrl57jvmhh2oemftjvamtc22htygi7vl52qmgbvqmk4.ipfs.nftstorage.link)|104.18.41.40|:443... connected.\n",
            "HTTP request sent, awaiting response... 200 OK\n",
            "Length: 2940928 (2.8M) [text/plain]\n",
            "Saving to: ‘retail2.sqlite’\n",
            "\n",
            "retail2.sqlite      100%[===================>]   2.80M  --.-KB/s    in 0.06s   \n",
            "\n",
            "2023-09-24 21:20:53 (50.3 MB/s) - ‘retail2.sqlite’ saved [2940928/2940928]\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain.agents import create_sql_agent\n",
        "from langchain.agents.agent_toolkits import SQLDatabaseToolkit\n",
        "from langchain.agents.agent_types import AgentType\n",
        "\n",
        "db = SQLDatabase.from_uri(\"sqlite:///retail2.sqlite\")\n",
        "agent_executor = create_sql_agent(\n",
        "    llm=llm,\n",
        "    toolkit=SQLDatabaseToolkit(db=db, llm=llm),\n",
        "    verbose=True,\n",
        "    agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",
        ")"
      ],
      "metadata": {
        "id": "6F8fwwM33SFw"
      },
      "execution_count": 16,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "agent_executor.run(\n",
        "    \"Show me the first row of reviews table\"\n",
        ")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 697
        },
        "id": "gT451nyH8Kkh",
        "outputId": "8fa02d1b-3148-4d66-d595-3f4ba7f906ef"
      },
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "\n",
            "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
            "\u001b[32;1m\u001b[1;3mAction: sql_db_list_tables\n",
            "Action Input: \u001b[0m\n",
            "Observation: \u001b[38;5;200m\u001b[1;3mbrands, finance, info, reviews, traffic\u001b[0m\n",
            "Thought:\u001b[32;1m\u001b[1;3m The reviews table looks most relevant, so I should query the schema of that table.\n",
            "Action: sql_db_schema\n",
            "Action Input: reviews\u001b[0m\n",
            "Observation: \u001b[33;1m\u001b[1;3m\n",
            "CREATE TABLE reviews (\n",
            "\tproduct_id VARCHAR(50), \n",
            "\trating VARCHAR(50), \n",
            "\treviews VARCHAR(50), \n",
            "\t\"Hour\" REAL, \n",
            "\tminute REAL, \n",
            "\treal_rating REAL, \n",
            "\treal_reviews REAL, \n",
            "\t\"Unnamed: 7\" VARCHAR(50)\n",
            ")\n",
            "\n",
            "/*\n",
            "3 rows from reviews table:\n",
            "product_id\trating\treviews\tHour\tminute\treal_rating\treal_reviews\tUnnamed: 7\n",
            "AH2430\tNone\tNone\t0.0\t0.0\tNone\t0.0\t\n",
            "G27341\t03:03:00\t1900-01-01 00:00:00\t3.0\t3.0\t3,3\t24.0\t\n",
            "CM0081\t02:06:00\t1900-01-01 13:00:00\t2.0\t6.0\t2,6\t37.0\t\n",
            "*/\u001b[0m\n",
            "Thought:\u001b[32;1m\u001b[1;3m I should query the first row of the reviews table.\n",
            "Action: sql_db_query\n",
            "Action Input: SELECT * FROM reviews LIMIT 1\u001b[0m\n",
            "Observation: \u001b[36;1m\u001b[1;3m[('AH2430', 'None', 'None', 0.0, 0.0, None, 0.0, '')]\u001b[0m\n",
            "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
            "Final Answer: The first row of the reviews table is ('AH2430', 'None', 'None', 0.0, 0.0, None, 0.0, '').\u001b[0m\n",
            "\n",
            "\u001b[1m> Finished chain.\u001b[0m\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\"The first row of the reviews table is ('AH2430', 'None', 'None', 0.0, 0.0, None, 0.0, '').\""
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "6.1 Modify data"
      ],
      "metadata": {
        "id": "Qh6xHtNQ6WMl"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "agent_executor.run(\n",
        "    \"Change all the real reviews to -1\"\n",
        ")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 697
        },
        "id": "EQ8ILTA136h1",
        "outputId": "95433b95-9f4d-4454-e88a-b9a73d5dc6cc"
      },
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "\n",
            "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
            "\u001b[32;1m\u001b[1;3mAction: sql_db_list_tables\n",
            "Action Input: \"\"\u001b[0m\n",
            "Observation: \u001b[38;5;200m\u001b[1;3mbrands, finance, info, reviews, traffic\u001b[0m\n",
            "Thought:\u001b[32;1m\u001b[1;3m I should look at the schema of the reviews table\n",
            "Action: sql_db_schema\n",
            "Action Input: \"reviews\"\u001b[0m\n",
            "Observation: \u001b[33;1m\u001b[1;3m\n",
            "CREATE TABLE reviews (\n",
            "\tproduct_id VARCHAR(50), \n",
            "\trating VARCHAR(50), \n",
            "\treviews VARCHAR(50), \n",
            "\t\"Hour\" REAL, \n",
            "\tminute REAL, \n",
            "\treal_rating REAL, \n",
            "\treal_reviews REAL, \n",
            "\t\"Unnamed: 7\" VARCHAR(50)\n",
            ")\n",
            "\n",
            "/*\n",
            "3 rows from reviews table:\n",
            "product_id\trating\treviews\tHour\tminute\treal_rating\treal_reviews\tUnnamed: 7\n",
            "AH2430\tNone\tNone\t0.0\t0.0\tNone\t0.0\t\n",
            "G27341\t03:03:00\t1900-01-01 00:00:00\t3.0\t3.0\t3,3\t24.0\t\n",
            "CM0081\t02:06:00\t1900-01-01 13:00:00\t2.0\t6.0\t2,6\t37.0\t\n",
            "*/\u001b[0m\n",
            "Thought:\u001b[32;1m\u001b[1;3m I should update the real_reviews column to -1\n",
            "Action: sql_db_query\n",
            "Action Input: \"UPDATE reviews SET real_reviews = -1\"\u001b[0m\n",
            "Observation: \u001b[36;1m\u001b[1;3m\u001b[0m\n",
            "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
            "Final Answer: All the real reviews in the reviews table have been changed to -1.\u001b[0m\n",
            "\n",
            "\u001b[1m> Finished chain.\u001b[0m\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'All the real reviews in the reviews table have been changed to -1.'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 18
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "agent_executor.run(\n",
        "    \"Show me the first row of reviews table\"\n",
        ")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 697
        },
        "id": "Z8oCoOAs7Puj",
        "outputId": "565a6e91-2f53-4667-a3db-3b51c849507b"
      },
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "\n",
            "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
            "\u001b[32;1m\u001b[1;3mAction: sql_db_list_tables\n",
            "Action Input: \u001b[0m\n",
            "Observation: \u001b[38;5;200m\u001b[1;3mbrands, finance, info, reviews, traffic\u001b[0m\n",
            "Thought:\u001b[32;1m\u001b[1;3m The reviews table looks most relevant, so I should query the schema of that table.\n",
            "Action: sql_db_schema\n",
            "Action Input: reviews\u001b[0m\n",
            "Observation: \u001b[33;1m\u001b[1;3m\n",
            "CREATE TABLE reviews (\n",
            "\tproduct_id VARCHAR(50), \n",
            "\trating VARCHAR(50), \n",
            "\treviews VARCHAR(50), \n",
            "\t\"Hour\" REAL, \n",
            "\tminute REAL, \n",
            "\treal_rating REAL, \n",
            "\treal_reviews REAL, \n",
            "\t\"Unnamed: 7\" VARCHAR(50)\n",
            ")\n",
            "\n",
            "/*\n",
            "3 rows from reviews table:\n",
            "product_id\trating\treviews\tHour\tminute\treal_rating\treal_reviews\tUnnamed: 7\n",
            "AH2430\tNone\tNone\t0.0\t0.0\tNone\t-1.0\t\n",
            "G27341\t03:03:00\t1900-01-01 00:00:00\t3.0\t3.0\t3,3\t-1.0\t\n",
            "CM0081\t02:06:00\t1900-01-01 13:00:00\t2.0\t6.0\t2,6\t-1.0\t\n",
            "*/\u001b[0m\n",
            "Thought:\u001b[32;1m\u001b[1;3m I now know the columns in the reviews table, so I can query the first row.\n",
            "Action: sql_db_query\n",
            "Action Input: SELECT * FROM reviews LIMIT 1\u001b[0m\n",
            "Observation: \u001b[36;1m\u001b[1;3m[('AH2430', 'None', 'None', 0.0, 0.0, None, -1.0, '')]\u001b[0m\n",
            "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n",
            "Final Answer: The first row of the reviews table is ('AH2430', 'None', 'None', 0.0, 0.0, None, -1.0, '').\u001b[0m\n",
            "\n",
            "\u001b[1m> Finished chain.\u001b[0m\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\"The first row of the reviews table is ('AH2430', 'None', 'None', 0.0, 0.0, None, -1.0, '').\""
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "6.2 Delete data"
      ],
      "metadata": {
        "id": "oYfJ2tY86X2h"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "agent_executor.run(\n",
        "    \"Delete all the reviews\"\n",
        ")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 624
        },
        "id": "QA5In1123wo3",
        "outputId": "ae4e6f79-e48b-4871-952c-1c709a9c6588"
      },
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\n",
            "\n",
            "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
            "\u001b[32;1m\u001b[1;3mAction: sql_db_list_tables\n",
            "Action Input: \u001b[0m\n",
            "Observation: \u001b[38;5;200m\u001b[1;3mbrands, finance, info, reviews, traffic\u001b[0m\n",
            "Thought:\u001b[32;1m\u001b[1;3m The reviews table is the most relevant, so I should query the schema of that table.\n",
            "Action: sql_db_schema\n",
            "Action Input: reviews\u001b[0m\n",
            "Observation: \u001b[33;1m\u001b[1;3m\n",
            "CREATE TABLE reviews (\n",
            "\tproduct_id VARCHAR(50), \n",
            "\trating VARCHAR(50), \n",
            "\treviews VARCHAR(50), \n",
            "\t\"Hour\" REAL, \n",
            "\tminute REAL, \n",
            "\treal_rating REAL, \n",
            "\treal_reviews REAL, \n",
            "\t\"Unnamed: 7\" VARCHAR(50)\n",
            ")\n",
            "\n",
            "/*\n",
            "3 rows from reviews table:\n",
            "product_id\trating\treviews\tHour\tminute\treal_rating\treal_reviews\tUnnamed: 7\n",
            "AH2430\tNone\tNone\t0.0\t0.0\tNone\t-1.0\t\n",
            "G27341\t03:03:00\t1900-01-01 00:00:00\t3.0\t3.0\t3,3\t-1.0\t\n",
            "CM0081\t02:06:00\t1900-01-01 13:00:00\t2.0\t6.0\t2,6\t-1.0\t\n",
            "*/\u001b[0m\n",
            "Thought:\u001b[32;1m\u001b[1;3m I should not delete any data from the database, so I should return \"I don't know\" as the answer.\n",
            "Final Answer: I don't know\u001b[0m\n",
            "\n",
            "\u001b[1m> Finished chain.\u001b[0m\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\"I don't know\""
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 20
        }
      ]
    }
  ]
}